package com.demo.service.uiv.impl;

import com.demo.common.util.bean.OrikaUtilsTool;
import com.demo.dao.uiv.entity.*;
import com.demo.dao.uiv.mapper.*;
import com.demo.service.uiv.UivPageInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Service
public class UivPageInfoServiceImpl implements UivPageInfoService {
    @Autowired
    private UivPageNodeMapper pageNodeMapper;

    @Autowired
    private UivComponentNodeMapper componentNodeMapper;

    //读取数据
    public UivPageInfo getPageInfo(Long pageId) {
        UivPageInfo r = new UivPageInfo();
        UivPageNode pageNode = pageNodeMapper.getPageNodeById(pageId);
        List<UivComponentNode> componentNodeList = componentNodeMapper.getComponentNodeByPageId(pageId);

        r.setPage(pageNode);
        r.setComponents(componentNodeList);

        //System.out.println(r);
        return r;
    }

    //插入数据
    public UivPageInfo savePageInfo(UivPageInfo info) {
        int i = 0, j = 0, k = 0;
        int row = 0;
        Long pageId = 0L, comId = 0L, propsId = 0L, dataId = 0L;
        Long motionId = 0L, eventId = 0L;
        UivComponentItem c = new UivComponentItem();
        UivComponentPropsItem props = new UivComponentPropsItem();
        UivComponentDataItem data = new UivComponentDataItem();
        UivComponentMotionItem motion = new UivComponentMotionItem();
        UivComponentEventItem event = new UivComponentEventItem();
        List<UivComponentPropsNode> propsList;
        List<UivComponentDataNode> dataList;
        List<UivComponentMotionNode> motionList;
        List<UivComponentEventNode> eventList;
        UivPageItem page = new UivPageItem();

        //PageItem存储
        page = OrikaUtilsTool.<UivPageNode, UivPageItem>convert(info.getPage(), UivPageItem.class);
        pageId = info.getPage().getId();
        page.setCreateUserId(info.getCreateUserId());
        page.setCreateTime(info.getCreateTime());
        page.setUpdateUserId(info.getUpdateUserId());
        page.setUpdateTime(info.getUpdateTime());
        if (pageId > 0) {
            row = pageNodeMapper.updatePageNode(page);
            if (row <= 0) {
                //System.out.println("page insert error");
                //获取刚插入记录的ID,mybatis返回的ID，useKeyGenerate
                pageNodeMapper.insertPageNode(page);
                pageId = page.getId();
                info.getPage().setId(pageId);
            }
        } else {
            //获取刚插入记录的ID,mybatis返回的ID，useKeyGenerate
            pageNodeMapper.insertPageNode(page);
            pageId = page.getId();
            info.getPage().setId(pageId);
        }

        //component存储
        for (i=0; i<info.getComponents().size(); i++) {
            //修改组件信息
            comId = info.getComponents().get(i).getId();
            info.getComponents().get(i).setPageId(pageId);
            c = OrikaUtilsTool.<UivComponentNode, UivComponentItem>convert(info.getComponents().get(i), UivComponentItem.class);
            c.setPageId(pageId);
            c.setCreateUserId(info.getCreateUserId());
            c.setCreateTime(info.getCreateTime());
            c.setUpdateUserId(info.getUpdateUserId());
            c.setUpdateTime(info.getUpdateTime());
            if (comId > 0) {
                row = componentNodeMapper.updateComponentNode(c);
                if (row <= 0) {
                    //获取刚插入记录的ID,mybatis返回的ID，useKeyGenerate
                    componentNodeMapper.insertComponentNode(c);
                    comId = c.getId();
                    info.getComponents().get(i).setId(comId);
                }
            } else {
                //获取刚插入记录的ID,mybatis返回的ID，useKeyGenerate
                componentNodeMapper.insertComponentNode(c);
                comId = c.getId();
                info.getComponents().get(i).setId(comId);
            }

            //props存储
            propsList = info.getComponents().get(i).getPropsItem();
            for (j=0; j<propsList.size(); j++) {
                props = OrikaUtilsTool.<UivComponentPropsNode, UivComponentPropsItem>convert(propsList.get(j), UivComponentPropsItem.class);
                //修改组件属性
                propsId = propsList.get(j).getId();
                propsList.get(j).setComId(comId);
                props.setComId(comId);
                props.setCreateUserId(info.getCreateUserId());
                props.setCreateTime(info.getCreateTime());
                props.setUpdateUserId(info.getUpdateUserId());
                props.setUpdateTime(info.getUpdateTime());

                //打印预览
                //System.out.println(props);
                if (propsId > 0) {
                    row = componentNodeMapper.updateComponentPropsNode(props);
                    if (row <= 0) {
                        componentNodeMapper.insertComponentPropsNode(props);
                        propsId = props.getId();
                        propsList.get(j).setId(propsId);
                    }
                } else {
                    componentNodeMapper.insertComponentPropsNode(props);
                    propsId = props.getId();
                    propsList.get(j).setId(propsId);
                }
            }

            //data存储
            dataList = info.getComponents().get(i).getDataItem();
            for (k=0; k<dataList.size(); k++) {
                //修改数据节点
                dataId = dataList.get(k).getId();
                dataList.get(k).setComId(comId);
                data = OrikaUtilsTool.<UivComponentDataNode, UivComponentDataItem>convert(dataList.get(k), UivComponentDataItem.class);
                data.setComId(comId);
                data.setCreateUserId(info.getCreateUserId());
                data.setCreateTime(info.getCreateTime());
                data.setUpdateUserId(info.getUpdateUserId());
                data.setUpdateTime(info.getUpdateTime());
                if (dataId > 0) {
                    row = componentNodeMapper.updateComponentDataNode(data);
                    if (row <= 0) {
                        componentNodeMapper.insertComponentDataNode(data);
                        dataList.get(k).setId(dataId);
                    }
                } else {
                    componentNodeMapper.insertComponentDataNode(data);
                    dataList.get(k).setId(dataId);
                }
            }

            //motion存储
            motionList = info.getComponents().get(i).getMotionItem();
            System.out.println(motionList);
            for (k=0; k<motionList.size(); k++) {
                //修改数据节点
                motionId = motionList.get(k).getId();
                motionList.get(k).setComId(comId);
                motion = OrikaUtilsTool.<UivComponentMotionNode, UivComponentMotionItem>convert(motionList.get(k), UivComponentMotionItem.class);
                motion.setComId(comId);
                motion.setCreateUserId(info.getCreateUserId());
                motion.setCreateTime(info.getCreateTime());
                motion.setUpdateUserId(info.getUpdateUserId());
                motion.setUpdateTime(info.getUpdateTime());
                if (motionId > 0) {
                    row = componentNodeMapper.updateComponentMotionNode(motion);
                    if (row <= 0) {
                        componentNodeMapper.insertComponentMotionNode(motion);
                        motionList.get(k).setId(motionId);
                    }
                } else {
                    componentNodeMapper.insertComponentMotionNode(motion);
                    motionList.get(k).setId(motionId);
                }
            }

            //event存储
            eventList = info.getComponents().get(i).getEventItem();
            System.out.println(eventList);
            for (k=0; k<eventList.size(); k++) {
                //修改数据节点
                eventId = eventList.get(k).getId();
                eventList.get(k).setComId(comId);
                event = OrikaUtilsTool.<UivComponentEventNode, UivComponentEventItem>convert(eventList.get(k), UivComponentEventItem.class);
                event.setComId(comId);
                event.setCreateUserId(info.getCreateUserId());
                event.setCreateTime(info.getCreateTime());
                event.setUpdateUserId(info.getUpdateUserId());
                event.setUpdateTime(info.getUpdateTime());
                if (eventId > 0) {
                    row = componentNodeMapper.updateComponentEventNode(event);
                    if (row <= 0) {
                        componentNodeMapper.insertComponentEventNode(event);
                        eventList.get(k).setId(eventId);
                    }
                } else {
                    componentNodeMapper.insertComponentEventNode(event);
                    eventList.get(k).setId(eventId);
                }
            }
        }

        return info;
    }


    //删除数据
    public boolean removePageInfo(List<Long> ids) {
        int i = 0;

        //删除组件
        for(i=0; i<ids.size(); i++) {
            componentNodeMapper.removeComponentNodeById(ids.get(i));
            componentNodeMapper.removeComponentPropsNodeByComId(ids.get(i));
            componentNodeMapper.removeComponentDataNodeByComId(ids.get(i));
            componentNodeMapper.removeComponentMotionNodeByComId(ids.get(i));
            componentNodeMapper.removeComponentEventNodeByComId(ids.get(i));
        }

        return true;
    }
}
